"""
FileName：login_page.py
Description：
Author：gaojiahui
Time：2025/8/29 18:37
Website：https://gitee.com/gao-jiahui
"""
from selenium.webdriver.common.by import By

from pages.base_page import BasePage


class LoginPage(BasePage):
    # 定位器
    ACCOUNT_LOGIN_BUTTON = (By.LINK_TEXT, "账号登录")
    USERNAME_FIELD = (By.ID, "username")
    PASSWORD_FIELD = (By.ID, "password")
    VERIFY_CODE_FIELD = (By.ID, "validcode")
    LOGIN_BUTTON = (By.CSS_SELECTOR, "div.account-login button.form-sub")
    ERROR_MESSAGE = (By.CSS_SELECTOR, "div.el-message--error")

    def select_account_login(self):
        self.click(self.ACCOUNT_LOGIN_BUTTON)

    def enter_username(self, username):
        self.send_keys(self.USERNAME_FIELD, username)

    def enter_password(self, password):
        self.send_keys(self.PASSWORD_FIELD, password)

    def enter_verify_code(self, verify_code):
        self.send_keys(self.VERIFY_CODE_FIELD, verify_code)

    def click_login_button(self):
        self.click(self.LOGIN_BUTTON)
        # 登录成功之后会重定向到首页
        from pages.home_page import HomePage
        return HomePage(self.driver)

    def get_error_message(self):
        return self.get_text(self.ERROR_MESSAGE)

    def login(self, username, password, verify_code):
        self.select_account_login()
        self.enter_username(username)
        self.enter_password(password)
        self.enter_verify_code(verify_code)
        return self.click_login_button()
